<?php

/*
 * This is to send email using sendgrid.
 * By: Savan Koradia
 * Email: koradia.savan@gmail.com
 * Blog: http://fedoracores.blogspot.in
 */

Class Sendgrid_Mail {

    public function __construct() {
        
    }

//    public function sendMail($username, $password, $mailServerPath, $fromEmail, $fromName, $toEmail, $toName, $subject, $body) {
//        $config = array('ssl' => 'tls',
//            'port' => '587',
//            'auth' => 'login',
//            'username' => $username,
//            'password' => $password);
//        $transport = new Zend_Mail_Transport_Smtp($mailServerPath, $config);
//        $mail = new Zend_Mail();
//        $mail->setFrom($fromEmail, $fromName);
//        $mail->addTo($toEmail, $toName);
//        $mail->setSubject($subject);
//        $mail->setBodyText($body);
//        $mail->send($transport);
//    }

    /**
     * 
     * @param type $data
     */
    public function sendMail($data) {
        $db = new Admin_Model_Api();
        $sendGridValues = $db->getApiByName('sendgrid');
        $values = $sendGridValues[0];
        $fromEmail = 'sjk.frills2008@gmail.com';
        $fromName = 'Savan Koradia @ Rent';
        $toEmail = $data['toEmail'];
        $toName = $data['toName'];
        $toName = ($toName == null) ? $toEmail : $toName;
        $subject = $data['subject'];
        $body = $data['body'];

        /**
         * Check is sendgrid is active or send from local server.
         */
        $mail = new Zend_Mail();
        $mail->setFrom($fromEmail, $fromName);
        $mail->addTo($toEmail, $toName);
        $mail->setSubject($subject);
//        $mail->setBodyText($body);
        $mail->setBodyHtml($body);
        if ($values['active'] == true) {
            $mailServerPath = $values['server_path'];
            $username = $values['api_key'];
            $password = $values['api_secret'];
            $config = array('ssl' => 'tls',
                'port' => '587',
                'auth' => 'login',
                'username' => $username,
                'password' => $password);
            $transport = new Zend_Mail_Transport_Smtp($mailServerPath, $config);
            $mail->send($transport);
        } else {//send from local server
            $mail->send();
        }
    }
    
}
